<?php

namespace app\api\controller;

use think\Request;

use think\Response;
use think\facade\Db;

;

class Recharge extends Wap
{
    /**
     * 充值列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $recharge_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('status', 1)->find();
            if ($recharge_order) {
                $post['store_id'] = $recharge_order['store_id'];
            }
            $list = Db::name('customer_recharge')->where('store_id', $post['store_id'])->where('status', 1)->where('mark', 1)->select();
            $data = array();
            foreach ($list as $i => $v) {
                $data[] = array(
                    'title' => $v['title'],
                    'id' => $v['id'],
                    'price' => $v['price'],
                    'give_price' => $v['give_price'],
                    'user_num' => $v['user_num'],
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    /**
     * 充值
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public
    function rechargeOrder(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            if (!isset($post['id'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }

            Db::name('customer_recharge_order')->where('status', 0)->where('user_id', $user['id'])->delete();
            if($post['chargeprice']==0) {
                $recharge = Db::name('customer_recharge')->where('id', $post['id'])->find();
                if ($recharge['user_num'] > 0) {
                    $c_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('level_id', $post['id'])->where('status', 1)->count();
                    if ($c_order >= $recharge['user_num']) {
                        $arr['code'] = 1001;
                        $arr['message'] = '超过活动次数';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }else{
                $recharge['price']=$post['chargeprice'];
                $recharge['give_price']=0;
            }
            $data['order_on'] = $this->orderOn();
            $data['create_time'] = time();
            $data['price'] = $recharge['price'];
            $data['level_id'] = $post['id'];
            $data['give_price'] = $recharge['give_price'];
            $data['user_id'] = $user['id'];
            $data['store_id'] = $post['store_id'];

            if (isset($post['store_id'])) {
                $room = Db::name('mini_room_list')->where('id', $post['store_id'])->find();

                $data['store_id'] = $post['store_id'];
                $da['if_fz'] = 0;
                if ($room['sub_mch_id'] !== null and $room['sub_mch_id'] !== '') {
                    $da['if_fz'] = 1;
                    $data['if_fz'] = 1;
                }
            }
            if (Db::name('customer_recharge_order')->insert($data)) {
                $arr['code'] = 200;
                $da['order_on'] = $data['order_on'];
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 会员列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function memberList(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();
            $list = Db::name('customer_member_level')->where('store_id', 'in', $post['store_id'])->where('mark', 1)->select();
            $data = array();
            foreach ($list as $i => $v) {
                $data[] = array(
                    'id' => $v['id'],
                    'name' => $v['name'],
                    'price' => $v['price'],
                    'gift_price' => $v['give_price'],
                );
            }
            $arr['data'] = $data;
            $arr['code'] = 200;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


    /**
     * 开通会员
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addLevel(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            $info = Db::name('customer_member_level')->where('id', $post['level_id'])->find();
            $data = array(
                'order_on' => $this->orderOn(),
                'level_id' => $post['level_id'],
                'user_id' => $user['id'],
                'create_time' => time(),
                'price' => $info['price'],
                'give_price' => $info['give_price'],
                'store_id' => $post['store_id'],
            );
            if (Db::name('customer_level_order')->insert($data)) {
                $arr['code'] = 200;
                $da['order_on'] = $data['order_on'];
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }
}